package base

import (
	"gin-base/base/db"
	"time"
)

type Model struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (this *Model) Save() bool {
	db := db.Open()
	defer db.Close()

	if db.NewRecord(this) {
		//创建
		db.Create(&this)

		return this.Create()
	} else {
		//更新
		this.Update()
		return true
	}

}

func (this *Model) Update() bool {
	db := db.Open()
	defer db.Close()
	db.Save(&this)

	return true
}

func (this *Model) Create() bool {
	db := db.Open()
	defer db.Close()

	db.Create(&this)

	return !db.NewRecord(this)
}

func (this *Model) Delete() bool {

	return true
}
